<?php
namespace Admin\Controller;

use Org\Util\String;
use Think\Controller;
class CardPasswordController extends ComController
{
   
    public function index() {
        $Ad = M('cardpassword');
        $where = [];
        $where['del_time'] = ['exp','is null'];
        $ads = $Ad->where($where)->select();
        $this->assign('ads', $ads);        
        $this->display();
    }
    
    //批量生成卡密32位，包括@#%!
    public function add() {
        $cardpassword = M('cardpassword');
        $id = $cardpassword->max('id');
        $start_id  = 0;
        if(!$id){
            $start_id = 1;
        }
        $passwords = [];
        for ($i=0;$i<10;$i++){
            $password = String::keyGen();
            $len = strlen($start_id);
            $password = substr($password,$len);
            $passwords[] = $start_id.$password;
            $start_id++;
        }
        $this->assign('passwords',$passwords);
        $this->display('form');
    }
    
    public function edit($id) {
        $id = intval($id);
        
        $adcate = $this->adcate();
        $ad = D('ad')->find($id);
        $this->assign('adcate', $adcate);
        $this->assign('ad', $ad);
        $this->display('form');
    }
    
    public function update() {
        $Ad = M('Ad');
         $data = I('post.data');
         $data['add_time'] = time();
         if($data['ad_id']){
           $Ad->data($data)->save();
           $this->success('编辑广告成功');
         }  else {
           $Ad->data($data)->add(); 
           $this->success('添加广告成功');
         }
         
    }
        
    public function store(){
        $passwords = I('post.passwords');
        $day = I('post.day');
        $data = [];
        for ($i=0;$i<count($passwords);$i++){
            $data[] = [
                'password'=>$passwords[$i],
                'day'=>$day,
            ];
        }

        $cardpassword = M('cardpassword');
        $cardpassword->addAll($data);
        $this->success('保存成功');
    }

    public function del(){
        $id = I('get.id');
        M('cardpassword')->where(['id'=>$id])->save(['del_time'=>time()]);
        $this->success('删除成功');
    }
    
}